var languageID = 1;
var categoryID = 1;
var from = 0;
var numberOfNews = 18;
var imageFinder;
$(document).ready(function(){
	initControls();
	loadNews();
});

function loadNews()
{
	$.get("/xjson/getnewslist", {l: languageID, c: categoryID, f: from, n: numberOfNews}, function(data){
		var jsonData = $.parseJSON(data);
		var newsTable = document.getElementById("listNewsTable");
		while (newsTable.rows.length > 1)
			newsTable.deleteRow(newsTable.rows.length - 1);
		if ((jsonData.code == 0) && (jsonData.data.length > 0))
		{
			for (var i = 0; i < jsonData.data.length; i++)
			{
				var row = newsTable.insertRow(newsTable.rows.length);
				var cellHTML = "";
				cellHTML += "<td align='center'>" + jsonData.data[i].ID + "</td>";
				cellHTML += "<td>" + jsonData.data[i].TITLE + "</td>";
				cellHTML += "<td align='center'>" + jsonData.data[i].PUBLISH_DATE + "</td>";
				cellHTML += "<td align='center'>" + jsonData.data[i].SOURCE + "</td>";
				cellHTML += "<td align='center'><a id='e" + jsonData.data[i].ID + "' href='#' class='editNews'>Sửa</a> | <a id='r" + jsonData.data[i].ID + "' href='#' class='removeNews'>Xóa</a></td>";
				row.innerHTML = cellHTML;
			}
			$(".editNews").click(function(){
				var id = $(this).attr("id").substr(1);
				editNews(id);
			});
			$(".removeNews").click(function(){
				var id = $(this).attr("id").substr(1);
			});
		}
	});
}

function initControls()
{
	// List news
	$("#newNewsButton").button();
	$("#listNewsCategoryID").change(function(){
		categoryID = $(this).val();
		loadNews();
	});
	$("#listNewsLanguageID").change(function(){
		languageID = $(this).val();
		loadNews();
	});
	
	$("#newNewsDialog").dialog({
		autoOpen: false,
		width: 1000,
		resizable: false,
		buttons: {
			"Thêm": function(){
				$("#newNewsErrorRow").css("display", "none");
				$.post("/xnews/add", $("#newNewsForm").serialize(), function(data){
					var returnData = $.parseJSON(data);
					if (returnData.code == 0)
					{
						loadNews();
						$("#newNewsDialog").dialog("close");
					}
					else
					{
						$("#newNewsErrorRow").css("display", "");
						$("#newNewsErrorMessage").html(returnData.data);
					}
				});
				return false;
			},
			"Đóng": function(){
				$(this).dialog("close");
			}
		},
		close: function(event, ui)
		{
			$("#newNewsLanguageVietnamese").attr("checked", "checked");
			$("#newNewsTitle").val("");
			$("#newNewsCategoryID").val(1);
			$("#newNewsIsHighlight").attr("checked", "checked");
			$("#newNewsIsHot").attr("checked", "checked");
			$("#newNewsPublishDate").val("");
			$("#newNewsSource").val("");
			$("#newNewsImageURL").val("");
			$("#newNewsSummary").val("");
			$("#newNewsContent").val("");
		}
	});
	$("#newNewsButton").click(function(){
		addNews();		
	})
	
	// New news
	$("#newNewsPublishDate").datepicker({
		dateFormat: "dd/mm/yy"
	});
	$("#newNewsImageSelect").button();
	$("#newNewsImageSelect").click(function(){
		window.KCFinder = {
			callBack: function(url) {
				window.KCFinder = null;
				$("#newNewsImageURL").val(url);
			}
		};
		window.open('/x/kcfinder/browse.php?type=files', 'kcfinder_textbox', 'status=0, toolbar=0, location=0, menubar=0, directories=0, ' + 'resizable=1, scrollbars=0, width=800, height=600');
	});
	$("#newNewsContent").ckeditor({
		height: 290,
		filebrowserBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserImageBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserFlashBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserWindowWidth: 640,
		filebrowserWindowHeight: 480
	});
	
	$("#editNewsDialog").dialog({
		autoOpen: false,
		width: 1000,
		resizable: false,
		buttons: {
			"Cập nhật": function(){
				$("#editNewsErrorRow").css("display", "none");
				$.post("/xnews/edit", $("#editNewsForm").serialize(), function(data){
					var returnData = $.parseJSON(data);
					if (returnData.code == 0)
					{
						loadNews();
						$("#editNewsDialog").dialog("close");
					}
					else
					{
						$("#editNewsErrorRow").css("display", "");
						$("#editNewsErrorMessage").html(returnData.data);
					}
				});
				return false;
			},
			"Đóng": function(){
				$(this).dialog("close");
			}
		}
	});
	$("#editNewsPublishDate").datepicker({
		dateFormat: "dd/mm/yy"
	});
	$("#editNewsImageSelect").button();
	$("#editNewsImageSelect").click(function(){
		window.KCFinder = {
			callBack: function(url) {
				window.KCFinder = null;
				$("#editNewsImageURL").val(url);
			}
		};
		window.open('/x/kcfinder/browse.php?type=files', 'kcfinder_textbox', 'status=0, toolbar=0, location=0, menubar=0, directories=0, ' + 'resizable=1, scrollbars=0, width=800, height=600');
	});
	$("#editNewsContent").ckeditor({
		height: 290,
		filebrowserBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserImageBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserFlashBrowseUrl: "/x/kcfinder/browse.php?type=files",
		filebrowserWindowWidth: 640,
		filebrowserWindowHeight: 480
	});
}

function addNews()
{
	$("#newNewsDialog").dialog("open");
}

function editNews(id)
{
	$("#editNewsID").val(id);
	$.get("/xjson/getnews", {i: id}, function(data){
		var jsonData = $.parseJSON(data);
		if (jsonData.data.LANGUAGE_ID == 1)
			$("#editNewsLanguageVietnamese").attr("checked", true);
		else if (jsonData.data.LANGUAGE_ID == 3)
			$("#editNewsLanguageChinese").attr("checked", true);
		$("#editNewsCategoryID").val(jsonData.data.CATEGORY_ID);
		$("#editNewsTitle").val(jsonData.data.TITLE);
		if (jsonData.data.IS_HIGHLIGHT == 1)
			$("#editNewsIsHighlight").attr("checked", true);
		else
			$("#editNewsIsHighlight").attr("checked", false);
		if (jsonData.data.IS_HOT == 1)
			$("#editNewsIsHot").attr("checked", true);
		else
			$("#editNewsIsHot").attr("checked", false);
		$("#editNewsPublishDate").val(jsonData.data.PUBLISH_DATE);
		$("#editNewsSource").val(jsonData.data.SOURCE);
		$("#editNewsImageURL").val(jsonData.data.IMAGE);
		$("#editNewsSummary").val(jsonData.data.SUMMARY);
		$("#editNewsContent").val(jsonData.data.CONTENT);
	});
	$("#editNewsDialog").dialog("open");
}